package Stage_1;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ReadServer {
    private String t = "E:\\Project_first\\sever.txt";
    private File address = new File(t);

    /**
     * 读取文件中的地址
     * @param file
     * @return
     */
    private List<String> readFile (File file){
        try (FileReader fileReader = new FileReader(file);
             BufferedReader bufferedReader = new BufferedReader(fileReader);){
            List<String> ip = new ArrayList<>();
            while (bufferedReader.ready()){
                String s = bufferedReader.readLine();
                ip.add(s);
            }
            return ip;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建线程池
     */
    private ThreadPoolExecutor threadPoolDetection( ){
        return new ThreadPoolExecutor(
                5,
                5,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10)
        );
    }
    /**
     *各个服务器地址判断是否可以连接
     * @return
     */
    private void allIsReceiveFile( ){
        List<String> ipAddress = readFile(address);
        ThreadPoolExecutor pool = threadPoolDetection();
        while (true) {
            ipAddress.forEach(o -> {
                Future<Integer> submit = pool.submit(new Callable<Integer>() {
                    @Override
                    public Integer call() throws Exception {
                        URL url = null;
                        try {
                            url = new URL(o);
                        } catch (MalformedURLException e) {
                            throw new RuntimeException(e);
                        }
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("GET");
                        System.out.println(Thread.currentThread().getName());
                        return connection.getResponseCode();
                    }
                });
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Integer integer = submit.get();
                    Maps maps = new Maps();
                    String s = maps.in().get(integer);
                    System.out.println(o + " " + integer + "\t" + s);
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }

    public ReadServer(){
        allIsReceiveFile();
    }
    public static void main(String[] args) {
        ReadServer readServer = new ReadServer();
    }
}
